home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / xlib05.zip / XBMTOOLS.ASM < prev    next >
Assembly Source File  |  1993-08-25  |  6KB  |  196 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XBMTOOLS
  3. ;
  4. ; Bitmap conversion / manipulation tools
  5. ;
  6. ; Compile with Tasm.
  7. ; C callable.
  8. ;
  9. ;
  10. ; ****** XLIB - Mode X graphics library                ****************
  11. ; ******                                               ****************
  12. ; ****** Written By Themie Gouthas                     ****************
  13. ;
  14. ; egg@dstos3.dsto.gov.au
  15. ; teg@bart.dsto.gov.au
  16. ;-----------------------------------------------------------------------
  17. COMMENT $
  18.  
  19.   This module implements a set of functions to manipulate both planar
  20.   bitmaps and linear bitmaps (as used by x_compile_bitmap):
  21.  
  22.   PLANAR BITMAPS
  23.  
  24.   Planar bitmaps as used by these functions have the following structure:
  25.  
  26.   BYTE 0                 The bitmap width in bytes (4 pixel groups) range 1..255
  27.   BYTE 1                 The bitmap height in rows range 1..255
  28.   BYTE 2..n1             The plane 0 pixels width*height bytes
  29.   BYTE n1..n2            The plane 1 pixels width*height bytes
  30.   BYTE n2..n3            The plane 2 pixels width*height bytes
  31.   BYTE n3..n4            The plane 3 pixels width*height bytes
  32.  
  33.   LINEAR BITMAPS
  34.  
  35.   Linear bitmaps have the following structure:
  36.  
  37.   BYTE 0                 The bitmap width in pixels  range 1..255
  38.   BYTE 1                 The bitmap height in rows   range 1..255
  39.   BYTE 2..n              The width*height bytes of the bitmap
  40.  
  41.  
  42.  
  43. $
  44.  
  45. LOCALS
  46. .286
  47.  
  48. include model.inc
  49. include xbmtools.inc
  50.  
  51.     .code
  52.  
  53.  
  54.  
  55. ;-----------------------------------------------------------------------
  56. ; x_pbm_to_bm
  57. ;
  58. ; This function converts a bitmap in the planar format to the linear format
  59. ; as used by x_compile_bitmap.
  60. ;
  61. ; WARNING: the source and destination bitmaps must be pre - allocated
  62. ;
  63. ; NOTE: This function can only convert planar bitmaps that are suitable.
  64. ;       If the source planar bitmap's width (per plane) is >= 256/4
  65. ;       it cannot be converted. In this situation an error code
  66. ;       BM_WIDTH_ERROR. On successful conversion 0 is returned.
  67. ;
  68. ; C callable as:
  69. ;    int x_pbm_to_bm(char far * source_pbm, char far * dest_bm);
  70. ;
  71. ; Written By Themie Gouthas
  72.  
  73. proc _x_pbm_to_bm
  74. ARG   src_pbm:dword,dest_bm:dword
  75.     push bp                  ; Preserve caller's stack frame
  76.     mov  bp,sp
  77.     push ds
  78.     push di
  79.     push si
  80.  
  81.     les  di,[dest_bm]     ; es:di -> destination bitmap
  82.     lds  si,[src_pbm]     ; ds:si -> source planar bitmap
  83.     lodsb                 ; load AL with source pbm pixel width per plane
  84.     mov  bl,al            ; save in CL
  85.     xor  ah,ah            ; convert to word
  86.     shl  ax,2             ; mult by 4 giving source image width
  87.     cmp  ax,255           ; if the result > 255 then we have exceeded
  88.     ja   @@WidthError     ; the max width of linear bm.
  89.  
  90.     stosb                 ; write do dest_bm
  91.  
  92.     lodsb                 ; tranfer source pbm height in pixels to
  93.     stosb             ;  dest_bm
  94.  
  95.     xor  ah,ah            ; convert to word
  96.     mul  bl               ; AX = AX * BL ie. total no. pixels per plane
  97.     mov  dx,di            ; save DI, the pointer to the destination bm
  98.     mov  bl,3             ; set plane loop counter (BL)
  99.  
  100. @@PlaneLoop:
  101.     mov  cx,ax            ; set CX to total number of pixels per plane
  102.  
  103. @@PixelLoop:
  104.     movsb                 ; transfer pixel
  105.     add  di,3             ; increment destination to compensate for plane
  106.     loop @@PixelLoop
  107.  
  108.     inc  dx               ; increment original di for next pixel plane
  109.     mov  di,dx            ; and restore di from incremented original
  110.     dec  bl               ; decrement plane counter
  111.     jns  @@PlaneLoop      ; loop if more planes left
  112.     xor  ax,ax
  113.     jmp  short @@Done
  114. @@WidthError:
  115.     mov  ax,1
  116.  
  117. @@Done:
  118.     pop  si
  119.     pop  di
  120.     pop  ds
  121.     pop  bp                  ;restore caller's stack frame
  122.     ret
  123. _x_pbm_to_bm endp
  124.  
  125.  
  126. ;-----------------------------------------------------------------------
  127. ; x_bm_to_pbm
  128. ;
  129. ; This function converts a bitmap in the linear format as used by
  130. ; x_compile_bitmap to the planar formap.
  131. ;
  132. ; WARNING: the source and destination bitmaps must be pre - allocated
  133. ;
  134. ; NOTE: This function can only convert linear bitmaps that are suitable.
  135. ;       If the source linear bitmap's width is not a multiple of 4
  136. ;       it cannot be converted. In this situation an error code
  137. ;       BM_WIDTH_ERROR. On successful conversion 0 is returned.
  138. ;
  139. ;
  140. ; C callable as:
  141. ;    int x_bm_to_pbm(char far * source_pbm, char far * dest_bm);
  142. ;
  143. ; Written By Themie Gouthas
  144.  
  145. proc _x_bm_to_pbm
  146. ARG src_bm:dword,dest_pbm:dword
  147.     push bp                  ; Preserve caller's stack frame
  148.     mov  bp,sp
  149.     push ds
  150.     push di
  151.     push si
  152.  
  153.     les  di,[dest_pbm]       ; es:di -> destination planar bitmap
  154.     lds  si,[src_bm]         ; ds:si -> source bitmap
  155.     lodsb                    ; load AX with source bitmap width
  156.     test al,03h              ; Check that width is a multiple of 4
  157.     jnz  @@WidthIncompatible
  158.     shr  al,2                ; divide by 4 giving width of plane
  159.     stosb                    ; store destination planar bitmap width
  160.     mov  bl,al               ;  and copy to bl
  161.     lodsb
  162.     stosb            ; Transfer source bitmap height to dest pbm
  163.     xor  ah,ah               ; Conver height to word
  164.     mul  bl                  ; calculate the total no. of pixels / plane
  165.     mov  dx,si               ; save source offset
  166.     mov  bl,3
  167.  
  168. @@PlaneLoop:
  169.     mov  cx,ax            ; set CX to total number of pixels per plane
  170.  
  171. @@PixelLoop:
  172.     movsb                 ; transfer pixel
  173.     add  si,3             ; increment src offset to compensate for plane
  174.     loop @@PixelLoop
  175.  
  176.     inc  dx               ; increment original si for next pixel plane
  177.     mov  si,dx            ; and restore si from incremented original
  178.     dec  bl               ; decrement plane counter
  179.     jns  @@PlaneLoop      ; loop if more planes left
  180.     xor  ax,ax
  181.     jmp  short @@Done
  182. @@WidthIncompatible:
  183.     mov  ax,1
  184.  
  185. @@Done:
  186.     pop  si
  187.     pop  di
  188.     pop  ds
  189.     pop  bp                  ;restore caller's stack frame
  190.     ret
  191. _x_bm_to_pbm endp
  192.  
  193.  
  194.     end
  195.  
  196.